package com.mask.mediaprojectionlibrary;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mask.mediaprojection.interfaces.MediaRecorderCallback;
import com.mask.mediaprojection.utils.MediaProjectionHelper;
import com.mask.mediaprojectionlibrary.notice.NotificationHelper;
import com.mask.mediaprojectionlibrary.util.LogUtil;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button btn_service_start;
    private Button btn_service_stop;
    private Button btn_media_recorder_start;
    private Button btn_media_recorder_stop;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == MediaProjectionHelper.REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                MediaProjectionHelper.getInstance().createVirtualDisplay(resultCode, data, this);
            } else {
                Toast.makeText(getApplication(), "请同意媒体投影服务", Toast.LENGTH_LONG).show();
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initNotification();
    }

    @Override
    protected void onDestroy() {
        MediaProjectionHelper.getInstance().stopService(this);
        super.onDestroy();
    }

    private void initView() {
        btn_service_start = findViewById(R.id.btn_service_start);
        btn_service_stop = findViewById(R.id.btn_service_stop);
        btn_media_recorder_start = findViewById(R.id.btn_media_recorder_start);
        btn_media_recorder_stop = findViewById(R.id.btn_media_recorder_stop);
    }

    private void initListener() {
        btn_service_start.setOnClickListener(v -> doServiceStart());
        btn_service_stop.setOnClickListener(v -> doServiceStop());
        btn_media_recorder_start.setOnClickListener(v -> doMediaRecorderStart());
        btn_media_recorder_stop.setOnClickListener(v -> doMediaRecorderStop());
    }

    private void initNotification() {
        Notification notification = NotificationHelper.getInstance()
                .createSystem()
                .setOngoing(true)// 常驻通知栏
                .setTicker("chao录制正在后台运行")
                .setContentTitle("chao录制正在后台运行")
                .setContentText("请启动投影服务")
                .setDefaults(Notification.DEFAULT_ALL)
                .build();
        MediaProjectionHelper.getInstance().setNotificationEngine(notification);
    }

    /**
     * 启动媒体投影服务
     */
    private void doServiceStart() {
        MediaProjectionHelper.getInstance().startService(this);
    }

    /**
     * 停止媒体投影服务
     */
    private void doServiceStop() {
        MediaProjectionHelper.getInstance().stopService(this);
    }

    /**
     * 开始屏幕录制
     */
    private void doMediaRecorderStart() {
        MediaRecorderCallback recorderCallback = new MediaRecorderCallback() {
            @Override
            public void onStarted(String msg) {
                super.onStarted(msg);

                Notification notification = NotificationHelper.getInstance()
                        .createSystem()
                        .setOngoing(true)// 常驻通知栏
                        .setTicker("屏幕录制已开始")
                        .setContentTitle("屏幕录制已开始")
                        .setContentText("正在录制屏幕")
                        .setDefaults(Notification.DEFAULT_ALL)
                        .build();
                MediaProjectionHelper.getInstance().setNotificationEngine(notification);
                MediaProjectionHelper.getInstance().showNotification();
            }

            @Override
            public void onCompleted(File file) {
                super.onCompleted(file);
                LogUtil.i("MediaRecorder onSuccess: " + file.getAbsolutePath());
                Toast.makeText(getApplication(), getString(R.string.content_media_recorder_result, file.getAbsolutePath()), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFail() {
                super.onFail();
                LogUtil.e("MediaRecorder onFail");
            }
        };
        MediaProjectionHelper.getInstance().startMediaRecorder(recorderCallback);

    }

    /**
     * 停止屏幕录制
     */
    private void doMediaRecorderStop() {
        MediaProjectionHelper.getInstance().stopMediaRecorder();
    }

}
